package fr.sinno.wms.batch.launcher;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BatchJobLauncher {

	private static Logger LOGGER = Logger.getLogger(BatchJobLauncher.class);
	
	public static void main(String[] args) {

		try {
			LOGGER.info("\n\n\n");
			LOGGER.info("####################################################################################");
			LOGGER.info("###################  DEBUT TRAITEMENT BATCH  #######################################");
			LOGGER.info("####################################################################################");

			ConfigurableApplicationContext cx  = 
				new ClassPathXmlApplicationContext("classpath:/bon-livraison-job.xml");
	
			Job job = (Job) cx.getBean("bonLivraisonJob");
			
			SimpleJobLauncher launcher = (SimpleJobLauncher)cx.getBean("jobLauncher");
			
			Map<String, JobParameter> map = new HashMap<String, JobParameter>();
			map.put("date.lancement",new JobParameter(new Date()));
			JobParameters params = new JobParameters(map);

			LOGGER.info("LANCEMENT DU JOB");
			JobExecution jobExecution = launcher.run(job, params);
		} catch (JobExecutionAlreadyRunningException e) {
			LOGGER.error(e.getMessage());
		} catch (JobRestartException e) {
			LOGGER.error(e.getMessage());
		} catch (JobInstanceAlreadyCompleteException e) {
			LOGGER.error(e.getMessage());
		} catch (JobParametersInvalidException e) {
			LOGGER.error(e.getMessage());
		}catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		
		
	}
}
